﻿using System.Web.UI;
using System.Web.UI.WebControls;
using Trentacular.Office.Server.Search.FacetedSearch;

namespace Trentacular.Office.Server.Web.UI.WebControls
{
    /// <summary>
    /// Generates a Panel that represents a facet.
    /// </summary>
    public class FacetDisplay : Panel
    {
        /// <summary>
        /// Gets or sets the facet.
        /// </summary>
        /// <value>The facet.</value>
        public Facet Facet { get; set; }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use
        /// composition-based implementation to create any child controls they contain
        /// in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Add(
                Facet.CustomRenderer ??
                new DefaultFacetRenderer { Facet = Facet }
                );

            base.CreateChildControls();
        }

        /// <summary>
        /// Adds specific attributes necessary for a facet.  Also, adds information
        /// about the background image, alignment, wrap, and direction to the list of
        /// attributes to render.
        /// </summary>
        /// <param name="writer">An <see cref="T:System.Web.UI.HtmlTextWriter"/> that
        /// represents the output stream to render HTML content on the client.</param>
        /// <exception cref="T:System.InvalidOperationException">
        /// The <see cref="P:System.Web.UI.WebControls.Panel.DefaultButton"/> property
        /// of the <see cref="T:System.Web.UI.WebControls.Panel"/> control must be the
        /// ID of a control of type
        /// <see cref="T:System.Web.UI.WebControls.IButtonControl"/>.
        /// </exception>
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            this.CssClass = Facet.IsActive ? "active facet" : "facet";
            
            writer.AddAttribute("key", Facet.Key);
            writer.AddAttribute("filter", Facet.FilterString);
            writer.AddAttribute("title", Facet.DisplayName);

            base.AddAttributesToRender(writer);
        }
    }
}
